﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InfoLayer;
using ControlLayer;

namespace GUI
{
    public partial class frmPhieuThuePhong : Form
    {
        private LoaiKhachHangControl loaiKhachHangControl;
        private PhieuThuePhongControl phieuThuePhongControl;
        private PhieuThuePhongInfo phieuThuePhongInfo;
        private PhongControl phongControl;
        private KhachHangControl khachHangControl;
        private KhachHangInfo khachHangInfo;
        private ChiTietPhieuThueControl chiTietPhieuThueControl;
        private ChiTietPhieuThueInfo chiTietPhieuThueInfo;
        private ThamSoControl thamSoControl;
        private ThamSoInfo thamSoInfo;

        public frmPhieuThuePhong()
        {
            InitializeComponent();
            loaiKhachHangControl = new LoaiKhachHangControl();
            phieuThuePhongControl = new PhieuThuePhongControl();
            phieuThuePhongInfo = new PhieuThuePhongInfo();
            phongControl = new PhongControl();
            khachHangControl = new KhachHangControl();
            khachHangInfo = new KhachHangInfo();
            chiTietPhieuThueControl = new ChiTietPhieuThueControl();
            chiTietPhieuThueInfo = new ChiTietPhieuThueInfo();
            thamSoControl = new ThamSoControl();
            thamSoInfo = new ThamSoInfo();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmPhieuThuePhong_Load(object sender, EventArgs e)
        {
            DataTable dt;
            dt = loaiKhachHangControl.LayBangDanhSachLoaiKhachHang();
            TenLoaiKhach.DataSource = dt;
            TenLoaiKhach.DisplayMember = "TenLoaiKhach";
            TenLoaiKhach.ValueMember = "MaLoaiKhach";
            TenLoaiKhach.AutoComplete = true;

             
            //Lay DS Phong con "trong"
            dt = phongControl.LayDSPhongTrong();
            comboBoxTenPhong.DataSource = dt;
            comboBoxTenPhong.DisplayMember = "TenPhong";
            comboBoxTenPhong.ValueMember = "MaPhong";
            comboBoxTenPhong.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBoxTenPhong.AutoCompleteSource = AutoCompleteSource.ListItems;
             
        }

        private void btnLapPhieu_Click(object sender, EventArgs e)
        {
            if (textBoxDonGiaThue.Text == "")
            {
                MessageBox.Show("Chọn tên phòng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            DataTable dt;
            //Them Phieu Thue Phong
            dt = phieuThuePhongControl.LayBangDanhSachPhong();
            if (dt.Rows.Count > 0)
                phieuThuePhongInfo.MaPhieuThue = phieuThuePhongControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 3);
            else
                phieuThuePhongInfo.MaPhieuThue = phieuThuePhongControl.TaoMaTuDong("MPT000", 3);
            phieuThuePhongInfo.MaPhong = comboBoxTenPhong.SelectedValue.ToString();
            phieuThuePhongInfo.NgayThuePhong = dateTimePickerNgayThuePhong.Value;
            phieuThuePhongInfo.DonGiaThue = Convert.ToInt32(textBoxDonGiaThue.Text);
            phieuThuePhongInfo.SoNgayThue = 0;
            phieuThuePhongInfo.ThanhTien = 0;
            phieuThuePhongInfo.MaHoaDon = "";

            

            

            //Them Khach Hang
            int Rows = dataGridViewDSKhachHang.Rows.Count;
            if (Rows != 0)
            {
                //Lay So Khach Toi Da
                dt = thamSoControl.LayBangThamSo();
                int soKhachToiDa = Convert.ToInt32(dt.Rows[0][0].ToString());
                if (Rows - 1 > soKhachToiDa)
                {
                    MessageBox.Show("Số khách tối đa là " + soKhachToiDa.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                for (int i = 0; i < Rows - 1; i++)
                {
                    dt = khachHangControl.LayBangDanhSachKhachHang();
                    if (dt.Rows.Count > 0)
                        khachHangInfo.MaKhachHang = khachHangControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 3);
                    else
                        khachHangInfo.MaKhachHang = khachHangControl.TaoMaTuDong("MKH000", 3);
                     
                     
                    if (dataGridViewDSKhachHang.Rows[i].Cells["TenKhach"].Value != null)
                        khachHangInfo.TenKhachHang = dataGridViewDSKhachHang.Rows[i].Cells["TenKhach"].Value.ToString();
                    else
                    {
                        MessageBox.Show("Nhập tên khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (dataGridViewDSKhachHang.Rows[i].Cells["TenLoaiKhach"].Value != null)
                        khachHangInfo.MaLoaiKhach = dataGridViewDSKhachHang.Rows[i].Cells["TenLoaiKhach"].Value.ToString();
                    else
                    {
                        MessageBox.Show("Chọn loại khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (dataGridViewDSKhachHang.Rows[i].Cells["CMND"].Value != null)
                        khachHangInfo.CMND = dataGridViewDSKhachHang.Rows[i].Cells["CMND"].Value.ToString();
                    if (dataGridViewDSKhachHang.Rows[i].Cells["DiaChi"].Value != null)
                        khachHangInfo.DiaChi = dataGridViewDSKhachHang.Rows[i].Cells["DiaChi"].Value.ToString();

                    khachHangControl.Insert(ref khachHangInfo);

                    //Them Bang Chi Tiet Phieu Thue
                    dt = chiTietPhieuThueControl.LayBangDanhSachPhong();
                    if (dt.Rows.Count > 0)
                        chiTietPhieuThueInfo.MaChiTietPhieu = chiTietPhieuThueControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 4);
                    else
                        chiTietPhieuThueInfo.MaChiTietPhieu = chiTietPhieuThueControl.TaoMaTuDong("MCTP000", 4);
                    chiTietPhieuThueInfo.MaPhieuThue = phieuThuePhongInfo.MaPhieuThue;
                    chiTietPhieuThueInfo.MaKhachHang = khachHangInfo.MaKhachHang;

                    chiTietPhieuThueControl.Insert(ref chiTietPhieuThueInfo);
                }
                    
            }

            //Update tinh trang cua phong, true = da thue,  false = chua thue, con trong
            phongControl.Update_TinhTrangPhong(phieuThuePhongInfo.MaPhong, true);

            //Them phieu thue phong
            phieuThuePhongControl.Insert(ref phieuThuePhongInfo);

            //Them thanh cong
            MessageBox.Show("Đã thêm thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //khong cho nhap Button Lap phieu nua
            btnLapPhieu.Enabled = false;
        }

        private void comboBoxTenPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt;
            phieuThuePhongInfo.MaPhong = comboBoxTenPhong.SelectedValue.ToString();
            dt = phieuThuePhongControl.LayDonGia(phieuThuePhongInfo.MaPhong);
            if (dt.Rows.Count > 0)
                textBoxDonGiaThue.Text = dt.Rows[0][0].ToString();
            else
                return;
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
             
            comboBoxTenPhong.ResetText();
            dateTimePickerNgayThuePhong.ResetText();
            dataGridViewDSKhachHang.Rows.Clear();
            textBoxDonGiaThue.Clear();
            frmPhieuThuePhong_Load(sender, e);
            btnLapPhieu.Enabled = true;
        }

        private void dataGridViewDSKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Hien thi STT Khach Hang
            if (dataGridViewDSKhachHang.CurrentRow.Cells[0].Value != null)
                return;
            else
                dataGridViewDSKhachHang.CurrentRow.Cells[0].Value = dataGridViewDSKhachHang.Rows.Count.ToString();
        }
    }
}
